package com.wanjl.ssm01.config;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;

/**
 * @author WanJl
 * @version 1.0
 * @title MybatisConfig
 * @description
 * @create 2024/11/10
 */
public class MybatisConfig {
    //设置mybatis连接数据库的环境配置
    @Bean
    public SqlSessionFactoryBean getSqlSessionFactoryBean(DataSource dataSource) {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        //设置别名
        sqlSessionFactoryBean.setTypeAliasesPackage("com.wanjl.ssm01.entity");
        sqlSessionFactoryBean.setDataSource(dataSource);
        return sqlSessionFactoryBean;
    }
    //设置mapper接口和xml的映射关系
    @Bean
    public MapperScannerConfigurer getMapperScannerConfigurer() {
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setBasePackage("com.wanjl.ssm01.mapper");
        return mapperScannerConfigurer;
    }
}
